﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace GetNewsForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.timer1.Interval = 100000;
            auto();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //MessageBox.Show(DateTime.Now.ToString("yyyy-MM"));
            work();
        }

        private void start_Click(object sender, EventArgs e)
        {
            int num = 0;
            if (int.TryParse(this.textBox1.Text, out num) )
            {
                if (num > 4)
                {
                    this.start.Enabled = false;
                   
                    work();
                    this.stop.Enabled = true; 
                    this.timer1.Interval = int.Parse(this.textBox1.Text)*1000*60;
                    this.timer1.Start();
                }
                else
                {
                    MessageBox.Show("时间间隔至少5分钟");
                }
            }
            else
            {
                MessageBox.Show("时间间隔必须为数字");
            }
        }

        private void stop_Click(object sender, EventArgs e)
        {
            this.timer1.Stop();
            this.start.Enabled = true;
            this.stop.Enabled = false;
        }

        private void auto()
        {
            this.start.Enabled = false;

            work();
            this.stop.Enabled = true;
            this.timer1.Interval = int.Parse(this.textBox1.Text) * 1000 * 60;
            this.timer1.Start();
        }

        private void work()
        {
            writeLog("开始获取新闻");
            try
            {
                this.stop.Enabled = false;
                GetNewsMain getNewMain = new GetNewsMain();

                //删除3天以前的新闻
                getNewMain.Delete();

                List<RssModel> rssList = getNewMain.GetRssList();
                List<NewsModel> allNews = new List<NewsModel>();
                List<NewsModel> rssNews = null;
                foreach (RssModel o in rssList)
                {
                    rssNews = getNewMain.GetNewsList(o);
                    if (rssNews != null && rssNews.Count > 0)
                    {
                        allNews.AddRange(rssNews);
                    }
                }

                List<NewsModel> sourceNews = getNewMain.GetALLNews();

                //移除重复的新闻
                allNews.RemoveAll(delegate(NewsModel o)
                {
                    if (sourceNews.Exists(delegate(NewsModel n)
                    {
                        if (n.NewsUrl.ToLower().Trim().Equals(o.NewsUrl.ToLower().Trim()))
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                });

                if (allNews.Count > 0)
                {
                    getNewMain.InsertNews(allNews);

                }
                this.stop.Enabled = true;
            }
            catch (Exception ex)
            {
                writeLog(ex.Message);
                this.timer1.Stop();
                this.start.Enabled = true;
                this.stop.Enabled = false;
            }
            
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="path"></param>
        /// <param name="content"></param>
        private void writeLog(string content)
        {
            try
            {

                string key = Application.ExecutablePath.Substring(0,Application.ExecutablePath.LastIndexOf('\\')+1);
                //string fullPath = key + "\\";  
                //FileInfo fi = new FileInfo(fullPath);
                string path = key.ToString() + "\\Log";
                if (!System.IO.Directory.Exists(path))
                    System.IO.Directory.CreateDirectory(path);
                path += "\\log_" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + ".txt";
                using (System.IO.StreamWriter sw = new System.IO.StreamWriter(path, true))
                {
                    sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + " " + content);
                }
            }
            catch (Exception ex)
            {
                writeLog(ex.Message);
                this.timer1.Stop();
                this.start.Enabled = true;
                this.stop.Enabled = false;
            }
        }
    }
}
